<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/29/029
 * Time: 11:04
 */

namespace app\common;
use think\Env;
class WeiXin
{
    private $APPID;
    private $APPSECERET;

    public function __construct()
    {
            $this->APPID = Env::get('WEIXIN.APPID');
            $this->APPSECERET = Env::get('WEIXIN.APPSECERET');
    }

    /** 获取accessToken
     * @return mixed
     */
   public  function  getAccessToken()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' .$this->APPID  . '&secret=' . $this->APPSECERET;
        $html = file_get_contents($url);
        $output = json_decode($html, true);
        $access_token = isset($output['access_token']) ? $output['access_token']: '';
        return $access_token;
    }

    /**
     * 发送活动开始订阅消息
     */
    public function sendSubscribeMsg($opeinid,$activeInfo,$type){
        $accessToken = $this->getAccessToken();
        $url  = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$accessToken;
        $param['touser'] = $opeinid;
        $param['template_id'] =  Env::get('ACTIVE_168.TEMPLATE_ID_'.$type);
        $param['page'] = Env::get('ACTIVE_168.PAGE_'.$type);//跳转活动详情页
        $param['data']['thing1']['value'] = Env::get('ACTIVE_168.MSG_NAME');//限时名称
        $param['data']['thing2']['value'] = Env::get('ACTIVE_168.MSG_TITLE_'.$type);//限时标题
        $param['data']['date4']['value'] = date('Y-m-d H:i:s',$activeInfo['xianshi_starttime']);//开始时间
        $param['data']['date5']['value'] = date('Y-m-d H:i:s',$activeInfo['xianshi_end_time']);//结束时间
        $param['data']['thing6']['value'] = Env::get('ACTIVE_168.MSG_DESC');
        $res = curl_post($url,json_encode($param));
        return json_decode($res,true);
    }

    /**
     * 发送端午优惠券过期提醒
     */
    public function sendSubscribeMsgDW($opeinid,$activeInfo,$type){
        $accessToken = $this->getAccessToken();
        $url  = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$accessToken;
        $param['touser'] = $opeinid;
        $param['template_id'] =  Env::get('ACTIVE_168.TEMPLATE_ID_'.$type);
        $param['page'] = Env::get('ACTIVE_168.PAGE_'.$type).'?goods_id='.Env::get('ACTIVE_168.PAGE_3_GOODS_ID');//跳转活动详情页
        $param['data']['thing1']['value'] = Env::get('ACTIVE_168.MSG_NAME_'.$type);//限时名称
        $param['data']['thing3']['value'] = Env::get('ACTIVE_168.MSG_TITLE_'.$type);//限时标题
        $param['data']['time2']['value'] = date('Y-m-d H:i:s',$activeInfo['use_end_time']);//结束时间
        $res = curl_post($url,json_encode($param));
        return json_decode($res,true);
    }
}